﻿namespace Whut.ObjectSharePointMapper.Internals
{
    using System;
    using System.Collections.Generic;
    using Microsoft.SharePoint;

    public class FindAllAction : SharePointAction
    {
        public SPWeb Web { get; set; }

        public FindAllAction(SPWeb web, MetaDataStore metaDataStore, SessionLevelCache sessionLevelCache, EntityHydrater entityHydrater)
            : base(metaDataStore, sessionLevelCache, entityHydrater)
        {
            this.Web = web;
        }

        // TODO może IList?
        public IEnumerable<TEntity> FindAll<TEntity>()
        {
            ListInfo listInfo = this.MetaDataStore.GetListInfoFor<TEntity>();
            SPList list = this.Web.GetListByRelativeUrl(listInfo.ListRelativeUrl);
            // TODO może SPQuery?
            return this.EntityHydrater.Hydrate<TEntity>(list.Items);
        }
    }
}
